package com.smedi.ismedi.inmail.core.application.query.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.inmail.core.application.command.LoginLogUserStaticsListCommand;
import com.smedi.ismedi.inmail.core.application.command.LoginLogUserStaticsPageCommand;
import com.smedi.ismedi.inmail.core.application.convert.AppConverter;
import com.smedi.ismedi.inmail.core.application.query.LoginLogUserStaticsQueryService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.LoginLogUserStatics;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.LoginLogUserStaticsEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.LoginLogUserStaticsMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 查询服务实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/30
 */
@Service
@AllArgsConstructor
@Slf4j
@SuppressWarnings("all")
public class LoginLogUserStaticsQueryServiceImpl implements LoginLogUserStaticsQueryService {
    private final LoginLogUserStaticsMapper loginLogUserStaticsMapper;
    private final AppConverter appConverter;

    @Override
    public Page<LoginLogUserStatics> page(LoginLogUserStaticsPageCommand loginLogUserStaticsPageCommand) {

        Page<LoginLogUserStaticsEntity> page = new Page<>(loginLogUserStaticsPageCommand.getCurrent(), loginLogUserStaticsPageCommand.getSize());

        // 对指定字段进行排序
        if ((null != loginLogUserStaticsPageCommand.getColumnName() || null != loginLogUserStaticsPageCommand.getIsAsc())) {
            String sortColumn = StrUtil.toUnderlineCase(loginLogUserStaticsPageCommand.getColumnName());
            page.addOrder(new OrderItem(sortColumn, loginLogUserStaticsPageCommand.getIsAsc()));
        }

        page.addOrder(new OrderItem("minute_30day", false))
                .addOrder(new OrderItem("id", true));
        LambdaQueryWrapper<LoginLogUserStaticsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<LoginLogUserStaticsEntity>()
                .eq(VerifyHelper.isAvailable(loginLogUserStaticsPageCommand.getOrganizationId()), LoginLogUserStaticsEntity::getOrganizationId, loginLogUserStaticsPageCommand.getOrganizationId());

        if (VerifyHelper.isAvailable(loginLogUserStaticsPageCommand.getNameOrEmployeeNum())) {
            lambdaQueryWrapper.and(wrapper -> {
                wrapper.like(LoginLogUserStaticsEntity::getPersonName, loginLogUserStaticsPageCommand.getNameOrEmployeeNum());
                wrapper.or().like(LoginLogUserStaticsEntity::getEmployeeNum, loginLogUserStaticsPageCommand.getNameOrEmployeeNum());
            });
        }

        Page<LoginLogUserStaticsEntity> loginLogUserStaticsEntityPage = loginLogUserStaticsMapper.selectPage(page, lambdaQueryWrapper);

        return appConverter.loginLogUserStaticsEntityToLoginLogUserStatics(loginLogUserStaticsEntityPage);
    }

    @Override
    public List<LoginLogUserStatics> list(LoginLogUserStaticsListCommand loginLogUserStaticsListCommand) {
        LambdaQueryWrapper<LoginLogUserStaticsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<LoginLogUserStaticsEntity>()
                .eq(VerifyHelper.isAvailable(loginLogUserStaticsListCommand.getOrganizationId()), LoginLogUserStaticsEntity::getOrganizationId, loginLogUserStaticsListCommand.getOrganizationId())
                .orderBy(true, false, LoginLogUserStaticsEntity::getMinute30Day)
                .orderBy(true, true, LoginLogUserStaticsEntity::getId);

        if (VerifyHelper.isAvailable(loginLogUserStaticsListCommand.getNameOrEmployeeNum())) {
            lambdaQueryWrapper.and(wrapper -> {
                wrapper.like(LoginLogUserStaticsEntity::getPersonName, loginLogUserStaticsListCommand.getNameOrEmployeeNum());
                wrapper.or().like(LoginLogUserStaticsEntity::getEmployeeNum, loginLogUserStaticsListCommand.getNameOrEmployeeNum());
            });
        }
        List<LoginLogUserStaticsEntity> loginLogUserStaticsEntityList = loginLogUserStaticsMapper.selectList(lambdaQueryWrapper);
        return appConverter.loginLogUserStaticsEntityToLoginLogUserStatics(loginLogUserStaticsEntityList);
    }
}
